#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;
// an ascending sequence
int binarySearch(int arr[],int left,int right,int x)
{
    int mid;
    while (left<=right)
    {
        mid=left+(right-left)/2;
        if (arr[mid]==x)
        {
            return mid;
        }
        else if (arr[mid]<x)
        {
            left=mid+1;
        }
        else
        {
            right=mid-1;
        }
    }
    return -1;
}
int main_100()
{
    srand((unsigned int)time(nullptr));
    const int n=20,m=20,x=rand()%m;
    int A[n]={0};
    for (int i=0;i<n ; ++i)
    {
        A[i]=rand()%m;
    }
    sort(A,A+n);
    for (int i=0;i<(n-1) ;++i )
    {
        printf("%d ",A[i]);
    }
    printf("%d\n",A[n-1]);
    printf("The index of %d is %d\n",x,binarySearch(A,0,n-1,x));
	return 0;
}
